
/*

单点应用，进入第三方系统待办列表页后，点击左上角返回按钮时，调 moaBridge.closeDoc({{'refresh':true,'appCode':''}})
单点应用，进入第三方系统待办列表页后，办理完工单时 调 moaBridge.refreshMOA()

统一待办进入，点击应用工单，进入第三方系统详情页后，点击左上角返回按钮时，调 moaBridge.closeDoc({{'refresh':true,'appCode':''}})
统一待办进入，点击应用工单，进入第三方系统详情页后，办理完成时调 moaBridge.closeDoc({{'refresh':true,'appCode':''}})

moaBridge.closeDoc({{'refresh':true,'appCode':'xxxx'}})


附件预览
var param = {'url':'xxxx',"fileName":"xxx",'uid':"xxx",'cookie':'xxxx'}
moaBridge.preview(param);
附件下载
moaBridge.download(param);

       


*/



var moaBridge = {

     closeDoc: function(param){

        if(is_android()){
          var paramJson = JSON.stringify(param)
          window.jsBridge.closeDoc(paramJson);
        }else if(is_iphone() || is_iPad()){
            window.webkit.messageHandlers.closeDoc.postMessage(param);
        }
    },

    refreshMOA:function(){
        if(is_android()){
            window.jsBridge.refreshMOA();
        }else if(is_iphone() || is_iPad()){
            window.webkit.messageHandlers.refreshMOA.postMessage(null);
        }
    },

    download:function(params){
      
        var fileName = params.fileName;
        var url = replaceurl(params.url,false);
        
        var uid = params.uid || '';
        var cookie = params.cookie || '';

        if(is_android()){
           
            window.jsBridge.download(url,fileName,uid,cookie);
        }
        if(is_iphone()){
           
            window.webkit.messageHandlers.download.postMessage({'url':url,'fileName':fileName,'uid':uid,'appid':cookie});
        }
        if(is_iPad()){
           
            window.webkit.messageHandlers.download.postMessage({'url':url,'fileName':fileName,'uid':uid,'appid':cookie});
        }

    },

    preview:function(params){
        console.log("传入的附件参数："+JSON.stringify(params));
       var fileName = params.fileName;
       var url = params.url;
       // var url = replaceurl(params.url,true);
       //  if(isImg(fileName)){
       //      url=replaceurl(params.url,false);
       //  }
       
       var uid = params.uid || '';
       var cookie = params.appId || '';

        if(is_android()){
            console.log("打开附件1111111");
            window.jsBridge.preview(url,fileName,uid,cookie);
        }
        if(is_iphone()){
            console.log("打开附件2222222");
            window.webkit.messageHandlers.preview.postMessage({'url':url,'fileName':fileName,'uid':uid,'appid':cookie});
        }
        if(is_iPad()){
            console.log("打开附333333333");
            window.webkit.messageHandlers.preview.postMessage({'url':url,'fileName':fileName,'uid':uid,'appid':cookie});
        }

    }

}


var ua = navigator.userAgent.toLowerCase();
//判断环境
function is_android(){
    if(ua.match(/android/i)=="android"&&ua.match(/iemobile/i)!="iemobile"){
         return true;
    } else {
         return false;
    }
}
function is_iphone(){
    if(ua.match(/iphone/i)=="iphone"&&ua.match(/iemobile/i)!="iemobile"){
         return true;
    } else {
         return false;
    }
}
function is_iPad(){
  if(ua.match(/iPad/i)=="iPad"||ua.match(/ipad/i)=="ipad"){
       return true;
  } else {
       return false;
  }
}

function replaceurl(url,type){

    var us=url.split('//');
    
    // let ns = [0x61,0x60,0x7e,0x68,0x67,0x7e,0x64,0x62,0x7e,0x61,0x63,0x66,0x6a,0x68,0x60,0x68,0x68,0x50];
    // let ws = ns;
    let ns = [0x1,0x0,0x1e,0x8,0x7,0x1e,0x1,0x3,0x1e,0x9,0x1,0xa,0x8,0x0,0x8,0x8,0x30];
    let ws = [0x11,0x12,0x12,0xd,0x12,0x10,0x1b,0xd,0x10,0x12,0xd,0x11,0x12,0x13,0x19,0x1b,0x13,0x1b,0x1b,0x23];

    var n = decodeString(ns);
    var w = decodeString(ws);
    
    var uss=us[1].split("/");
    uss.splice(0,1);
    return us[0]+"//"+`${type?n:w}`+'/'+uss.join('/');

}

function decodeString(strs) {
    var size = strs.length;
    var seed = strs[size - 1];
    var decodeStr = '';
    var j = 0;
    do{
        var a = strs[j] ^ seed;
        decodeStr += String.fromCharCode(a);
        j++;
    }while(j < size - 1);
    return decodeStr;
}

//图片文件的后缀名
var imgExt = new Array(".png",".jpg",".jpeg",".bmp",".gif");

//获取文件名后缀名
String.prototype.extension = function(){
    var ext = null;
    var name = this.toLowerCase();
    var i = name.lastIndexOf(".");
    if(i > -1){
    var ext = name.substring(i);
    }
    return ext;
}

//判断Array中是否包含某个值
function contain(obj){
    for(var i=0; i<this.length; i++){
        if(this[i] === obj)
            return true;
    }
    return false;
};

//判断是否图片文件
function isImg(filename){
    var ext = filename.extension();
    if(imgExt.contain(ext)){        
        return true;
    }
    return false;
}

// window.moaBridge = moaBridge;
export default moaBridge



